package com.demo.arch.datapulsedemo.v2_pipeline.controller;

import com.demo.arch.datapulsedemo.common.TaskResult;
import com.demo.arch.datapulsedemo.v2_pipeline.core.ReportTaskPipeline;
import com.demo.arch.datapulsedemo.v2_pipeline.core.StepHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * V2版本报表控制器（Pipeline模式）
 * 
 * @author DataPulse Team
 */
@Slf4j
@RestController
@RequestMapping("/v2")
public class ReportControllerV2 {

  @Autowired
  private ReportTaskPipeline pipeline;

  /**
   * 执行报表任务
   * 
   * 测试URL:
   * - 日报: http://localhost:8080/v2/report?type=日报
   * - 周报: http://localhost:8080/v2/report?type=周报
   * - 月报: http://localhost:8080/v2/report?type=月报
   */
  @GetMapping("/report")
  public TaskResult generateReport(@RequestParam(defaultValue = "日报") String type) {
    log.info("=== 收到V2 Pipeline报表生成请求 ===");
    log.info("请求参数 - 报表类型: {}", type);

    TaskResult result = pipeline.execute(type);

    log.info("=== V2 Pipeline任务执行结果: {} ===", result.isSuccess() ? "成功" : "失败");
    return result;
  }

  /**
   * 健康检查接口
   */
  @GetMapping("/health")
  public TaskResult health() {
    return TaskResult.success("V2 Pipeline服务正常运行");
  }

  /**
   * 查询Pipeline步骤信息
   */
  @GetMapping("/steps")
  public Map<String, Object> getSteps() {
    List<StepHandler> steps = pipeline.getSteps();

    Map<String, Object> result = new HashMap<>();
    result.put("total", steps.size());
    result.put("steps", steps.stream()
        .map(step -> Map.of(
            "name", step.getName(),
            "order", step.getOrder()))
        .collect(Collectors.toList()));

    return result;
  }
}
